package com.example.fw;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;

import com.example.tests.ContactObjectClass;

public class ContactHelper extends HelperWithWebDriverBase{
		
	public ContactHelper (ApplicationManager manager) {
		super(manager);
	}
	
	public void contactCreationTest(ContactObjectClass validContact) {
		manager.getNavigationHelper().openMainPage();
		manager.getNavigationHelper().goToContactCreatePage();
		fillContactForm(validContact, true);
		submitContactCreation();
		backToHomePage();
	}
	
	public void contactModificationTest(ContactObjectClass validContact) {
		manager.getNavigationHelper().openMainPage();
		modifyFirstContact();
		fillContactForm(validContact, false);
		updateContactData();
		backToHomePage();
	}
	
	public void contactCanBeRemovalTest() {
		manager.getNavigationHelper().openMainPage();
		modifyFirstContact();
		deleteContact();
		backToHomePage();
	}
			
	protected void backToHomePage() {
		click(By.linkText("home page")); 
	}

	protected void submitContactCreation() {
		click(By.name("submit"));
	}
	
	protected void updateContactData() {
		click(By.name("update"));
	}
	
	protected void deleteContact() {
		click(By.xpath("(//input[@name='update'])[2]"));
	}

	protected void modifyFirstContact() {
		click(By.cssSelector("img[alt=\"Details\"]"));
		click(By.name("modifiy"));
	}

	protected void fillContactForm(ContactObjectClass groupContactParameterObject, boolean fillGroup) {
		type(groupContactParameterObject.cntFirstName, "firstname");
		type(groupContactParameterObject.cntLastName, "lastname");
		type(groupContactParameterObject.cntaddress, "address");
		type(groupContactParameterObject.cntHome, "home");
		type(groupContactParameterObject.cntMobile, "mobile");
		type(groupContactParameterObject.cntWorkPhone, "work");
		type(groupContactParameterObject.cntEmail1, "email");
		type(groupContactParameterObject.cntEmail2, "email2");
		type(groupContactParameterObject.cntByear, "byear");
		type(groupContactParameterObject.cntAddressSecondary, "address2");
		type(groupContactParameterObject.cntPhoneSecondary, "phone2");
		selectCurrentField(groupContactParameterObject.bday, By.name("bday"));
		selectCurrentField(groupContactParameterObject.bmonth, By.name("bmonth"));
		if (fillGroup){
		selectCurrentField(groupContactParameterObject.new_group, By.name("new_group"));
		}
		else {
//			
		}
		
	}

}
